//
//  LSCustHubView.m
//  HappyStore
//
//  Created by 陈锋 on 2016/11/15.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSCustHubView.h"

#define bubble_width 180 // 泡泡控件的宽度
#define bubble_height 120// 泡泡控件的高度
#define bubble_icon_width 60 // 泡泡控件中的图标边长
#define bubble_padding 17 // 泡泡控件的顶部内边距，即图标距离顶部的长度
#define bubble_icon_title_space 0 // 泡泡控件中图标和标题的空隙

@implementation LSCustHubView{

    UIImageView * _iconImageView;
    UILabel * _titleLabel;
    BOOL _isShowing;//是否正在显示中
    NSMutableDictionary<NSString *,LSCuatHub *>* infoDic;
    /// @brief 当前正在显示的泡泡信息对象
    LSCuatHub * _currenInfo;
    /// @brief 当前自定义动画绘图的图层
    CAShapeLayer * _currentDrawLayer;
    /// @brief 当前使用的图片帧动画计时器
    NSTimer * _currentTimer;
    /// @brief 蒙版view
    UIView * _maskView;
    
    CGFloat closeKey;//关闭key，用来做关闭时候的延迟验证，设置自动关闭之后，若在关闭之前发出了其他的info的bubble，通过修改这只值保证不关闭其他的infobubble
    NSInteger frameAnimationPlayIndax;// 帧动画播放的下标索引

}

static LSCustHubView *defaultBubbleView;

+(LSCustHubView *)defaultBubbleView{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        defaultBubbleView = [[LSCustHubView alloc]init];
    });
    return defaultBubbleView;
}

-(instancetype)init{

    if (self = [super init]) {
        UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
        self.frame = CGRectMake(keyWindow.centerX, keyWindow.centerY, 0, 0);
        self->infoDic = [NSMutableDictionary dictionary];
        
        self.clipsToBounds = YES;
        self->_iconImageView = [[UIImageView alloc]init];
        self->_iconImageView.clipsToBounds = YES;
        self->_titleLabel = [[UILabel alloc]init];
        self->_titleLabel.textAlignment = NSTextAlignmentCenter;
        self->_titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self->_titleLabel setNumberOfLines:0];
        //初始化蒙版控件
        self->_maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        self->_maskView.hidden = YES;
        
        [self addSubview:self->_iconImageView];
        [self addSubview:self->_titleLabel];
    }
    return self;

}

/**
 *  @author 1em0nsOft LiuRi
 *  @date 2016-08-30 16:08:53
 *
 *  @brief 显示指定的信息模型对应的泡泡控件
 */
-(void)showWithInfo:(LSCuatHub *)info{

    self->_currenInfo = info;
    self->closeKey = self->_currenInfo.key;// 保存当前要关闭的key，防止关闭不需要关闭的bubble
    
    if (info.isShowMaskView)
        [[UIApplication sharedApplication].keyWindow addSubview:_maskView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    //弹簧动画改变外观
    [UIView animateWithDuration:0.4 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 1;
        if (self->_currentDrawLayer) {
            [self->_currentDrawLayer removeFromSuperlayer];
        }
        
        self.frame = [info calBubbleViewFrame];
        self->_iconImageView.frame = [info calIconViewFrame];
        self->_titleLabel.frame = [info calTitleViewFrame];
        self->_titleLabel.text = info.title;
        self->_titleLabel.font = [UIFont systemFontOfSize:info.titleFontSize];
        self.layer.cornerRadius = info.cornerRadius;
        
        if (info.iconArray == nil || info.iconArray.count == 0) {
            //显示自定义动画
            _iconImageView.image = [[UIImage alloc]init];
            _currentDrawLayer= [CAShapeLayer layer];
            _currentDrawLayer.fillColor = [UIColor clearColor].CGColor;
            _currentDrawLayer.frame = self->_iconImageView.bounds;
            [self->_iconImageView.layer addSublayer:_currentDrawLayer];
            [_currentTimer invalidate];
            dispatch_async(dispatch_get_main_queue(), ^{
                info.iconAnimation(_currentDrawLayer);
            });
        }else if(info.iconArray.count == 1){// 显示单张图片
        
            [_currentTimer invalidate];
            self->_iconImageView.image = info.iconArray[0];
        
        }else{// 逐帧连环动画
            frameAnimationPlayIndax = 0;// 帧动画播放索引归零
            self->_iconImageView.image = _currenInfo.iconArray[0];
            self->_currentTimer = [NSTimer scheduledTimerWithTimeInterval:info.frameAnimationTime target:self selector:@selector(frameAnimationPlayer) userInfo:nil repeats:YES];
        
        
        }
        // maskView
        if (_currenInfo.isShowMaskView && _maskView.hidden) {
            //本次需要显示，之前已经隐藏
            _maskView.alpha = 0;
            _maskView.hidden = NO;
        }
        
        self->_titleLabel.textColor = info.titleColor;
        [self setBackgroundColor:info.backgroundColor];
        _currentDrawLayer.strokeColor = info.iconColor.CGColor;
        self->_maskView.backgroundColor = self->_currenInfo.maskColor;
        
        self->_maskView.alpha = self->_currenInfo.isShowMaskView ? 1 : 0;
    } completion:^(BOOL finished) {
        if (!_currenInfo.isShowMaskView) {
            _maskView.hidden = YES;
            [_maskView removeFromSuperview];
        }
    }];
    

    
//    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionTransitionCurlUp animations:^{
//        self->_titleLabel.textColor = info.titleColor;
//        [self setBackgroundColor:info.backgroundColor];
//        _currentDrawLayer.strokeColor = info.iconColor.CGColor;
//        self->_maskView.backgroundColor = self->_currenInfo.maskColor;
//    } completion:^(BOOL finished) {
//        
//    }];
//    
}

-(void)showWithInfo:(LSCuatHub *)info autoCloseTime:(CGFloat)time{

    [self showWithInfo:info];
    [self performSelector:@selector(hide) withObject:self afterDelay:time + 0.2];

}

/**
 帧动画播放器 - NSTimer调用
 */
- (void)frameAnimationPlayer{
    self->_iconImageView.image = _currenInfo.iconArray[frameAnimationPlayIndax];
    frameAnimationPlayIndax = (frameAnimationPlayIndax + 1) % _currenInfo.iconArray.count;
}

/**
 *  @author 1em0nsOft LiuRi
 *  @date 2016-08-30 16:08:40
 *
 *  @brief 隐藏当前泡泡控件
 */
-(void)hide{

    if (self->closeKey == self->_currenInfo.key) {//要关闭的key没有变化，可以关闭
        // 动画缩放，更改透明度使其动画隐藏
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
            self->_maskView.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            [self->_maskView removeFromSuperview];
        }];
    }

}
@end
